package dao;

import java.awt.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;

import util.JDBCUtil;

import domain.ActorJob;
import domain.User;
import ex.DatabaseException;

public class ActedDAO {
	private Connection conn;
	private PreparedStatement stmt;
	private ResultSet rs;

	private static final String CREATE_ACTED = "insert into acted (uid,mid,status,ajid) values (?,?, ?, ?) ";
	private static final String SELECT_ACTED_BY_AJID = "select * from acted where ajid = ?";
	private static final String EDIT_STATUS_OF_ACTED_BY_UID_AND_AJID = "update acted set status=? where uid=? and ajid=?";
	private static final String DELETE_ACTED_BY_AJID = "delete from acted  where ajid=?";

	UserDAO udao = new UserDAO();

	public void insertActed(User user, ActorJob actorJob)
			throws DatabaseException {
		try {
			conn = JDBCUtil.dequeueConn();
			Map<User, Integer> applies = selectActedUsersByAjid(actorJob
					.getAjid());
			if (applies.containsKey(user) && applies.get(user) == -1) {

				stmt = conn
						.prepareStatement(EDIT_STATUS_OF_ACTED_BY_UID_AND_AJID);
				stmt.setInt(1, 0);
				stmt.setLong(2, user.getUid());
				stmt.setLong(3, actorJob.getAjid());
				stmt.execute();
				JDBCUtil.enqueuConn(conn);
				stmt.close();
			} else {
				stmt = conn.prepareStatement(CREATE_ACTED);
				stmt.setLong(1, user.getUid());
				stmt.setLong(2, actorJob.getMid());
				stmt.setInt(3, 0);
				// stmt.setString(4, );
				stmt.setLong(4, actorJob.getAjid());

				stmt.execute();
				JDBCUtil.enqueuConn(conn);
				stmt.close();
			}
		} catch (SQLException e) {
			throw new DatabaseException(
					"Problem with inserting actor job information! "
							+ e.getMessage());
		}
	}

	public Map<User, Integer> selectActedUsersByAjid(long ajid)
			throws DatabaseException {
		Map<User, Integer> users = new HashMap<User, Integer>();
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(SELECT_ACTED_BY_AJID);
			stmt.setLong(1, ajid);

			rs = stmt.executeQuery();
			while (rs.next()) {
				long uid = rs.getLong("uid");
				int s = rs.getInt("status");
				User user = udao.selectUserByUid(uid);
				if (user != null) {
					users.put(user, s);
				}
			}
			JDBCUtil.enqueuConn(conn);
			stmt.close();
		} catch (SQLException e) {
			throw new DatabaseException(
					"Problem with inserting actor job information! "
							+ e.getMessage());
		}
		return users;
	}

	public void acceptApply(User user, ActorJob aj) throws DatabaseException {
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(EDIT_STATUS_OF_ACTED_BY_UID_AND_AJID);
			stmt.setInt(1, 1);
			stmt.setLong(2, user.getUid());
			stmt.setLong(3, aj.getAjid());

			stmt.execute();
			JDBCUtil.enqueuConn(conn);
			stmt.close();
		} catch (SQLException e) {
			throw new DatabaseException(
					"Problem with inserting actor job information! "
							+ e.getMessage());
		}
	}

	public void rejectApply(User user, ActorJob aj) throws DatabaseException {
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(EDIT_STATUS_OF_ACTED_BY_UID_AND_AJID);
			stmt.setInt(1, -1);
			stmt.setLong(2, user.getUid());
			stmt.setLong(3, aj.getAjid());

			stmt.execute();
			JDBCUtil.enqueuConn(conn);
			stmt.close();
		} catch (SQLException e) {
			throw new DatabaseException(
					"Problem with inserting actor job information! "
							+ e.getMessage());
		}
	}
	
	public void deleteActedByAjid(long ajid) throws DatabaseException {
		try {
			conn = JDBCUtil.dequeueConn();
			stmt = conn.prepareStatement(DELETE_ACTED_BY_AJID);
			stmt.setLong(1, ajid);			

			stmt.execute();
			JDBCUtil.enqueuConn(conn);
			stmt.close();
		} catch (SQLException e) {
			throw new DatabaseException(
					"Problem with inserting actor job information! "
							+ e.getMessage());
		}
	}

	// public LinkedList<User> searchActor(int heightMin, int heightMax, int
	// weightMin, int weightMax, String hairColor)
	// {
	//
	// }
}
